package com.lingjtx.oss.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lingjtx.common.thread.commom.ApiKeyFilter;
import com.lingjtx.common.thread.commom.NettyServerExceptionResolver;
import com.lingjtx.common.thread.commom.RestResponseConfig;
import io.minio.MinioClient;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.imageio.ImageIO;

@Configuration
public class OssConfig {

    @Value("${minio.endpoint}")
    private String endpoint;

    @Value("${minio.accessKey}")
    private String accessKey;

    @Value("${minio.secretKey}")
    private String secretKey;

    @Bean
    public MinioClient minioClient() {
        return MinioClient.builder()
                .endpoint(endpoint)
                .credentials(accessKey, secretKey)
                .build();
    }

    @Bean
    public ApiKeyFilter apiKeyFilter() {
        return new ApiKeyFilter();
    }

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(RestResponseConfig config) {
        return config.jacksonObjectMapper();
    }

    @Bean
    public NettyServerExceptionResolver resolver() {
        return new NettyServerExceptionResolver();
    }

    @PostConstruct
    public void registerImageReaders() {
        ImageIO.scanForPlugins();
    }
}
